// // 如果允许拷贝，可能会出现浅拷贝问题：
// RecordSet rs1 = executeQuery("SELECT * FROM table");
// RecordSet rs2 = rs1;  // 浅拷贝

// // 两个对象共享同一_impl，可能产生意外的副作用
// rs1.next();  // 可能影响rs2的迭代状态


#pragma once
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
class SqlConnection;
/**
 * 查询结果集
 * 非线程安全
 */

class RecordSetImpl;
class RecordSet
{
   public:
    using RowRecord = std::map<std::string, std::string>;
    using RowRecordPtr = std::shared_ptr<RowRecord>;

    RecordSet(const RecordSet &) = delete;
    RecordSet &operator=(const RecordSet &) = delete;

    RecordSet(RecordSet &&) = default;
    RecordSet &operator=(RecordSet &&) = default;
    ~RecordSet() = default;

    operator bool() const { return _impl != nullptr; }
    bool operator==(std::nullptr_t) const { return _impl == nullptr; }
    bool operator!=(std::nullptr_t) const { return _impl != nullptr; }

    bool begin();
    bool next();
    bool eof();
    int count() const;
    int columnCount() const;
    RowRecordPtr
    getRow(int index = 0) const;
    std::string getFieldValueString(const std::string &fieldName) const;
    std::string getFieldValueString(int index) const;
    int64_t affectedRows() const;
    template <class T>
    T getFieldValue(const std::string &fieldName) const
    {
        std::string str = getFieldValueString(fieldName);
        T value;
        std::istringstream iss(str.c_str());
        iss >> value;
        return value;
    }
    template <class T>
    T getFieldValue(int index) const
    {
        std::string str = getFieldValueString(index);
        T value;
        std::istringstream iss(str.c_str());
        iss >> value;
        return value;
    }

   private:
    friend class SqlConnection;
    friend class Sqlitepp;

    explicit RecordSet(std::shared_ptr<RecordSetImpl> impl);

    std::shared_ptr<RecordSetImpl> _impl;
};